unit ListUtils;

interface

uses
  Classes, Windows, sysUtils;



Procedure List_AddString(lst:TStrings; display, Code:string); overload;
Procedure List_AddString(lst:TStrings; display:String; Code:integer); overload;

function  List_GetCodeS(lst:TStrings; index:integer):String;
function  List_GetCodeI(lst:TStrings; index:integer; const defaultValue:integer=-1):Integer;

procedure List_Clear(lst:TStrings);
procedure List_ClearObjects(lst:TStrings);
Function  List_Indexof(lst:TStrings; Code:String): integer;
Procedure List_UpdateString(lst:TStrings; index:integer; code:String);
Procedure List_Clone(Src, Dst:TStrings);
procedure List_DeleteItem(lst:TStrings; index:integer);


implementation

Procedure List_Clone(Src, Dst:TStrings);
var
  I: Integer;
begin
  for I := 0 to Src.Count - 1 do
    List_AddString(Dst, Src.Strings[i], List_GetCodeS(src, i) );
end;
  
Procedure List_UpdateString(lst:TStrings; index:integer; code:String);
begin
  {$ifndef unicode}
  DisposeStr(PString(lst.Objects[index]));
  {$else}
  DisposeStr(pAnsiString(lst.Objects[index]));
  {$endif}
  lst.Objects[index] := TObject(LongInt(NewStr(code)));
end;


Function  List_Indexof(lst:TStrings; Code:String): integer;
var
  I: Integer;
begin
  Result := -1;
  for I := 0 to lst.Count - 1 do
    if List_GetCodeS(lst, i) = Code then
    begin
      Result := i;
      break;
    end;
end;

procedure List_DeleteItem(lst:TStrings; index:integer);
begin
  {$ifndef unicode}
  DisposeStr(PString(lst.Objects[index]));
  {$else}
  DisposeStr(pAnsiString(lst.Objects[index]));
  {$endif}
  lst.Delete( index );
end;

procedure List_Clear(lst:TStrings);
begin
  List_ClearObjects(lst);
  lst.Clear;
end;

procedure List_ClearObjects(lst:TStrings);
var
  Index: Integer;
begin
  for Index := lst.Count -1 downto 0 do begin
    If lst.Objects[Index] = nil Then Continue;
    {$ifndef unicode}
    DisposeStr(PString(lst.Objects[index]));
    {$else}
    DisposeStr(pAnsiString(lst.Objects[index]));
    {$endif}
  end;
end;


function List_GetCodeS(lst:TStrings; index:integer):String;
begin
  if lst.Count = 0 then Result := ''
  {$ifdef unicode}
  else Result := pAnsiString(lst.Objects[index])^;
  {$else}
  else Result := pString(lst.Objects[index])^;
  {$endif}
end;

function  List_GetCodeI(lst:TStrings; index:integer; const defaultValue:integer=-1):Integer;
var
  Buff: String;
begin
  Buff := List_GetCodeS(lst, index);

  Result := StrToIntDef(Buff, defaultValue);
end;


Procedure List_AddString(lst:TStrings; display, Code:string); overload;
begin
  lst.AddObject(display, TObject(LongInt(NewStr(code))));
end;

Procedure List_AddString(lst:TStrings; display:String; Code:integer); overload;
var
  Buff: string;
begin
  Buff := IntToStr(code);
  lst.AddObject(display, TObject(LongInt(NewStr(buff))));
end;


end.
